package com.neu.his.service.registFee02.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neu.his.bo.fee.FeeRec;
import com.neu.his.bo.inspection.InsItem;
import com.neu.his.bo.inspection.InspectionRec;
import com.neu.his.bo.inspection.InspectionSend;
import com.neu.his.core.Result;
import com.neu.his.entity.DiagRecords;
import com.neu.his.entity.NonDrugDisturb;
import com.neu.his.entity.Patient;
import com.neu.his.mapper.DiagRecordMapper;
import com.neu.his.mapper.NonDrugDisturbMapper;
import com.neu.his.mapper.NonDrugMapper;
import com.neu.his.mapper.PatientMapper;
import com.neu.his.service.registFee02.InspectionService;
import com.neu.his.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Service
public class InspectionImpl implements InspectionService {
    @Autowired
    private NonDrugDisturbMapper nonDrugDisturbMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DiagRecordMapper diagRecordMapper;
    @Autowired
    private NonDrugMapper nonDrugMapper;

    @Override
    public Result getItem(FeeRec feeRec){
        InspectionSend inspectionSend = new InspectionSend();
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Patient::getIdNumber,feeRec.getPatientId());
        List<Patient> patients = patientMapper.selectList(queryWrapper);
        if(patients.size() == 0){
            return Result.fail("病历号不存在！");
        }
        inspectionSend.setPatient(patients.get(0));
        List<DiagRecords> diagRecordsList = diagRecordMapper.selectList(
                new QueryWrapper<DiagRecords>().lambda()
                        .eq(DiagRecords::getPatientId,patients.get(0).getId())
        ) ;
        List<InsItem> list = new ArrayList<>();
        for(DiagRecords diagRecords:diagRecordsList){
            QueryWrapper<NonDrugDisturb> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(NonDrugDisturb::getDiagId,diagRecords.getId());
            List<NonDrugDisturb> nonDrugDisturbs = nonDrugDisturbMapper.selectList(queryWrapper1);
            for(NonDrugDisturb nonDrugDisturb:nonDrugDisturbs){
                //如果已经付费但为进行，则选中
                if( (nonDrugDisturb.isHaveDeliver() == false)){
                    InsItem insItem = new InsItem();
                    insItem.setItemId(nonDrugDisturb.getId());
                    if(nonDrugDisturb.isPaid() == true){
                        insItem.setIsPaid("是");
                    } else {
                        insItem.setIsPaid("否");
                    }
                    insItem.setDiagId(diagRecords.getId());
                    insItem.setItemName(
                            nonDrugMapper.selectById(nonDrugDisturb.getNonDrugId()).getNonDrugName()
                    );
                    list.add(insItem);
                }
            }
        }
        PageList pageList = new PageList(list,feeRec.getCurPage(),feeRec.getPageSize());
        inspectionSend.setData(pageList);
        return Result.success(inspectionSend);

    }


    @Override
    public Result setInspection(InspectionRec inspectionRec){
        UpdateWrapper<NonDrugDisturb> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(NonDrugDisturb::getId,inspectionRec.getData().getItemId())
                .set(NonDrugDisturb::getNonDrugResult,inspectionRec.getResult());
        updateWrapper.set("have_deliver",true);
        nonDrugDisturbMapper.update(null,updateWrapper);
        return Result.success();
    }
}
